{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "752ca88f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "numpy      : 1.23.4\n",
      "scipy      : 1.9.3\n",
      "torch      : 1.12.1\n",
      "pomegranate: 0.14.8\n",
      "\n",
      "Compiler    : GCC 11.2.0\n",
      "OS          : Linux\n",
      "Release     : 4.15.0-197-generic\n",
      "Machine     : x86_64\n",
      "Processor   : x86_64\n",
      "CPU cores   : 8\n",
      "Architecture: 64bit\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "import scipy\n",
    "import torch\n",
    "\n",
    "from sklearn.datasets import make_blobs\n",
    "\n",
    "from torchegranate.distributions import *\n",
    "from torchegranate.bayes_classifier import BayesClassifier\n",
    "\n",
    "from sklearn.naive_bayes import GaussianNB, BernoulliNB\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn; seaborn.set_style('whitegrid')\n",
    "\n",
    "numpy.random.seed(0)\n",
    "numpy.set_printoptions(suppress=True)\n",
    "\n",
    "%load_ext watermark\n",
    "%watermark -m -n -p numpy,scipy,torch,pomegranate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d323e83",
   "metadata": {},
   "source": [
    "### Gaussian Naive Bayes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db6dc2d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "n, d, k = 200000, 500, 50\n",
    "\n",
    "X, y = make_blobs(n_samples=n, n_features=d, centers=k, cluster_std=0.75, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4a980a8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "787 ms ± 22.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
      "872 ms ± 16.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit model_sklearn = GaussianNB().fit(X, y)\n",
    "%timeit model_pom = BayesClassifier([Normal(covariance_type='diag') for i in range(k)]).fit(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "24f702bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20.9 s ± 24.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
      "15.6 s ± 152 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "model_sklearn = GaussianNB().fit(X, y)\n",
    "model_pom = BayesClassifier([Normal(covariance_type='diag') for i in range(k)]).fit(X, y)\n",
    "\n",
    "%timeit model_sklearn.predict(X)\n",
    "%timeit model_pom.predict(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0f5959c",
   "metadata": {},
   "source": [
    "### Bernoulli Naive Bayes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1a73281c",
   "metadata": {},
   "outputs": [],
   "source": [
    "n, d, k = 200000, 200, 25\n",
    "\n",
    "X = numpy.random.choice(2, size=(n, d))\n",
    "y = numpy.random.choice(k, size=(n,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f711516d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14 s ± 242 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
      "359 ms ± 905 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit model_sklearn = BernoulliNB().fit(X, y)\n",
    "%timeit model_pom = BayesClassifier([Bernoulli() for i in range(k)]).fit(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bab540b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "628 ms ± 12.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
      "3.01 s ± 35.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "model_sklearn = BernoulliNB().fit(X, y)\n",
    "model_pom = BayesClassifier([Bernoulli() for i in range(k)]).fit(X, y)\n",
    "\n",
    "%timeit model_sklearn.predict(X)\n",
    "%timeit model_pom.predict(X)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
